package com.le.tester.webpage.masterCode;


import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 描述：equals源码
 */
public class EqualsDemo {

    private char[] value;

    public EqualsDemo() {
    }

    public EqualsDemo(char[] value) {
        this.value = Arrays.copyOf(value, value.length);
    }

    public boolean f1(Object obj) {
        //内存地址如果都是958，那就是相等的
        if (this == obj) {
            return true;
        }
        //这边使用的是要string
        if (obj instanceof String) {
            String otherString = (String) obj;
            int n = value.length;
            if (n == otherString.length()) {
                //构造器初始了一个char数组
                char[] c1 = value;
                char[] c2 = otherString.toCharArray();
                int i = 0;
                while (n-- != 0) {
                    if (c1[i] != c2[i])
                        return false;
                    i++;
                }
                return true;
            }
        }
        return false;

    }

    public void testReplace() {
        String str = "hello word";
        System.out.println("替换之前：" + str);
        //还是需要重新的赋值，并不能直接的替换内存地址,替换了之后会在原先的string上进行操作
        //str = str.replace('l', 'd');
        System.out.println("使用了replace：" + str);
        //str = str.replaceAll("l", "d");
        System.out.println("使用了replaceAll：" + str);
        str = str.replaceFirst("l", "");
        System.out.println("使用了replaceFirst：" + str);

    }

    public void testSplit() {
        String s = ",a, , b c ,";
        //依旧还是得重新赋值得到,空值是拆分不掉的
        //看逗号前后，不会显示被拆分的字段
        //limit看你要得到数组要分成几个部分，小于等于0代表部限制
        String[] strings = s.split(",");
        List<String> list = Splitter.on(",")
                .trimResults()//去掉空格
                .omitEmptyStrings()//去掉空值
                .splitToList(s);//不稳定
        System.out.println(list);
        System.out.println("it is not use guava " + Arrays.toString(strings));
    }

    public void testJoin() {
        String s = ",a, , b c ,";
        String s1 = "boo,far,coo";

        //直接把s的值给覆盖了
        String join = String.join(",", s).join(":", s1);
        System.out.println(join);


        Joiner joiner = Joiner.on(",")
                //不要空值
                .skipNulls();
        String result = joiner.join("hello", null, "china");
        System.out.println("it is used Joiner guava " + result);

        //一种自动去除空值的用法
        List<String> list = Lists.newArrayList("hello", null, "china",null);
        System.out.println("Joiner auto remove null,result is " + joiner.join(list));

    }

    public static void main(String[] args) {
//        char[] test = {'1', '2', '3', '4'};
//        //默认从0开始复制
//        char[] chars = Arrays.copyOf(test, 5);
//        System.out.println(chars);
        EqualsDemo equalsDemo = new EqualsDemo();
//        equalsDemo.testReplace();
        equalsDemo.testJoin();
    }
}
